# -*- coding: utf-8 -*-
from zope.interface import Interface, Attribute
from zope.app.folder.interfaces import IFolder
from zope import schema
    
from zope.i18nmessageid import MessageFactory
_ = MessageFactory("dbformlib")
    
class IDBFormlibContentAdd(Interface):
    """ Interfejs uzywany przy dodawaniu obiektu
    """
    title = schema.TextLine(
        title=u"Title",
        required=False,
        )
    
    connectionName = schema.Choice(
        title=u"Connection",
        vocabulary='ConnectionVocabulary',
        description=u"Identifier of the default connection id",
        required=True,
        default='PUBLIC')
    
class IDBFormlibContent(IFolder):
    """DBCRUDable Entity
        C - create
        R - read
        U - update
        D - delete
    """
    
    connectionName = Attribute("RDBMS connection name")
    
    def get_values():
        """ Get values from underlying storage and (also)
            cache them
        """
        
    def set_values(crud_id, ckey, data):
        """ Save values to underlying storage
            and update cache
        """
        
    def get_use_cache_id():
        """ get request parameter for use cache
        """
        
    def _rollback():
        """ Rollbacks database transaction
        """
        
    def _get_form_data(crud_id='', in_params={}):
        """ To be overriden by descendant classes.
            Function should contain code that 
            gets data from underlying storage, eg.
            calls to SQLScripts
        """
    
    def _set_form_data(crud_id='', data={}, errors=[]):
        """ To be overriden by descendant classes.
            Function should contain code that 
            saves data into underlying storage, eg.
            calls to SQLScripts. If there are errors during
            save (eg. from RDBMS) they should be handled here 
            and error message has to be appended to errors list.
        """
        
    def nextURL(crud_id=''):
        """ redirection url
        """
        
    def setup():
        """ Function that should be used to create SQLScripts
            for this conent and such things
        """
        
class IDataAdapter(Interface):
    """ This adapter is a interface between view and underlying content object
        In general it provides three methods:
         get_data - to retrieve data from database (internally uses cache)
         set_data - to save data to database
         apply_data - to save data to database only if there were changes    
    """
    
    def _use_cached():
        """ Determine whether form data should be taken from cached
            result
        """
    
    def _get_entry_values(ignore_request=False):
        """ get entry values from the request (entry values)
        """
        
    def _get_data(crud_id, use_cache=True, in_params={}):
        """ Retrieves form data
            If cache is not used then it has to:
            1. retrieve values of entry fields from request
            2. call _get_data function with entry_fields values
               as parameters
            3. return data
        """
        
    def get_data(actions, ignore_request):
        """ Used to get data for widgets in getWidgetsData
            Uses get_data to retrieve data, but first
            checks if data should be taken from cache or not.
        """
        
    def apply_data(form_fields, data):
        """ Saves data (calls user defined function), 
            but first checks if there were changes
        """
        
    def set_data(form_fields, data):
        """ Saves data (calls user defined function)
        """
        
    def __getattr__(name):
        """ Returns value for given field name
            Value is retrieved from database or cache
        """
        
    def __getattr__(name, value):
        """ Sets value of field with id 'name'.
            Data persist only during one request.
            It is not stored at RDBMS
        """
    
        
class IEntryField(Interface):
    """ Marker for my field
    """

from zope.interface import classImplements
from fields import EntryField
classImplements(EntryField, IEntryField)